//
//  TextAreaGroupTableViewCell.m
//  mcrmSAAS
//
//  Created by wjxfhxy on 15-3-13.
//  Copyright (c) 2015年 GuangZhouXuanWu. All rights reserved.
//

#import "TextAreaGroupTableViewCell.h"

@interface TextAreaGroupTableViewCell ()<UITextViewDelegate>
{
    TextInputCellTitleOffsetType _offsetType;
    CGFloat _offsetX;
    
@public
    UILabel *_maskLabel;
    UILabel *_titleLabel;
    XUITextView *_textView;
}

@end

@implementation TextAreaGroupTableViewCell

-(instancetype)init
{
    return [self initWithFrame:CGRectZero];
}

-(id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    
    _offsetX = 10;
    
    CGSize size = [@"*" stringSizeWithFont:TextInputCellTextFont];
    
    _maskLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, size.width, 10)];
    _maskLabel.backgroundColor = [UIColor clearColor];
    _maskLabel.textColor = [UIColor redColor];
    _maskLabel.text = @"*";
    _maskLabel.hidden = YES;
    _maskLabel.font = TextInputCellTextFont;
    _maskLabel.textAlignment = NSTextAlignmentCenter;
    [self.contentView addSubview:_maskLabel];
    
    size = [NSString charSizeWithFont:TextInputCellTitleFont];
    _titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, size.width * 6, size.height)];
    _titleLabel.backgroundColor = [UIColor clearColor];
    _titleLabel.textColor = [UIColor blackColor];
    _titleLabel.font = [UIFont systemFontOfSize:13];
    _titleLabel.textAlignment = NSTextAlignmentRight;
    [self.contentView addSubview:_titleLabel];
    
    _textView = [[XUITextView alloc]initWithFrame:CGRectMake(0, 0, 100, 90)];
    _textView.backgroundColor = [UIColor clearColor];
    _textView.contentInset = UIEdgeInsetsMake(-9, -4, -9, 0);
    _textView.hiddenDoneButton = NO;
    _textView.font = TextInputCellTextFont;
    _textView.delegate = self;
    [self.contentView addSubview:_textView];
    
    return self;
}

-(void)layoutSubviews
{
    [super layoutSubviews];
    
    CGFloat offsetX = _offsetX;
    
    if(_maskLabel != nil)
    {
        _maskLabel.left = offsetX;        
        offsetX = _maskLabel.right + 5;
    }

    _titleLabel.left = offsetX;
    _titleLabel.centerY = [TextInputGroupTableViewCell caleHeight] / 2 - 5;
    
    _maskLabel.centerY = _titleLabel.centerY + 3;
    
    _textView.left = _titleLabel.right + 10;
    _textView.top = _titleLabel.top;
    _textView.width = self.contentView.width - _textView.left - 10;
    _textView.height = self.contentView.height - _textView.top * 2;
}

-(void)setAllowEmpty:(BOOL)allowEmpty
{
    _maskLabel.hidden = allowEmpty;
}

-(void)setTitle:(NSString *)title
{
    _titleLabel.text = title;
    _textView.placeHolder = [NSString stringWithFormat:@"请输入%@",title];
    _textView.placeHolderFont = [UIFont systemFontOfSize:14];
    _textView.placeHolderColor = [UIColor colorWithR:214 G:214 B:214 A:1];
}

-(NSString*)title
{
    return _titleLabel.text;
}

-(NSString*)text
{
    return _textView.text;
}

-(void)setText:(NSString *)text
{
    _textView.text = text;
}

-(void)setTextMaxLength:(NSUInteger)maxLength
{
    _textView.textInputMaxLength = maxLength;
}

-(void)setTextInputOffsetType:(TextInputCellTitleOffsetType)offsetType
{
    _offsetType = offsetType;
    switch(offsetType)
    {
        case TextInputCellTitleOffsetTypeDefault:
        {
            break;
        }
        case TextInputCellTitleOffsetTypeSixChar:
        {
            [_maskLabel removeFromSuperview];
            _maskLabel = nil;
            CGSize size = [NSString charSizeWithFont:TextInputCellTitleFont];
            _titleLabel.width = size.width * 6;
            break;
        }
        case TextInputCellTitleOffsetTypeFourChar:
        {
            [_maskLabel removeFromSuperview];
            _maskLabel = nil;
            CGSize size = [NSString charSizeWithFont:TextInputCellTitleFont];
            _titleLabel.width = size.width * 4;
            break;
        }
        case TextInputCellTitleOffsetTypeMaskFourChar:
        {
            CGSize size = [NSString charSizeWithFont:TextInputCellTitleFont];
            _titleLabel.width = size.width * 4;
            break;
        }
        default:
        {
            [_maskLabel removeFromSuperview];
            _maskLabel = nil;
            break;
        }
    }
    [self setNeedsLayout];
}

-(void)setTextInputOffsetType:(TextInputCellTitleOffsetType)offsetType offSetX:(CGFloat)x
{
    _offsetX = x;
    [self setTextInputOffsetType:offsetType];
}

- (void)textViewDidBeginEditing:(UITextView *)textView
{
    if([(id<TextAreaGroupTableViewCellDelegate>)self.delegate respondsToSelector:@selector(textAreaGroupTableViewCell:textViewDidBeginEditing:)])
        [(id<TextAreaGroupTableViewCellDelegate>)self.delegate textAreaGroupTableViewCell:self textViewDidBeginEditing:textView];
}

- (void)textViewDidEndEditing:(UITextView *)textView
{
    if([(id<TextAreaGroupTableViewCellDelegate>)self.delegate respondsToSelector:@selector(textAreaGroupTableViewCell:textViewDidEndEditing:)])
        [(id<TextAreaGroupTableViewCellDelegate>)self.delegate textAreaGroupTableViewCell:self textViewDidEndEditing:textView];
}

//- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
//{
//    if([(id<TextAreaGroupTableViewCellDelegate>)self.delegate respondsToSelector:@selector(textAreaGroupTableViewCell:textView:shouldChangeCharactersInRange:replacementString:)])
//        return [(id<TextAreaGroupTableViewCellDelegate>)self.delegate textAreaGroupTableViewCell:self textView:textView shouldChangeCharactersInRange:range replacementString:text];
//    return YES;
//}

+(CGFloat)caleHeight
{
    return 90;
}

@end

//@implementation TextAreaStaticGroupTableViewCell : TextAreaGroupTableViewCell
//
//-(id)initWithFrame:(CGRect)frame
//{
//    self = [super initWithFrame:frame];
//    
//    _textView.editable = NO;
//    
//    return self;
//}
//
//@end